Skip to content

Conversation

bolshakov-a
Copy link
Contributor

No description provided.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 6, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 6, 2025

@llvm/pr-subscribers-clang

Author: Andrey Ali Khan Bolshakov (bolshakov-a)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/162134.diff

4 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1-1)
  • (modified) clang/lib/AST/DeclPrinter.cpp (+5-1)
  • (modified) clang/test/AST/ast-print-record-decl.c (+7)
  • (modified) clang/unittests/AST/DeclPrinterTest.cpp (+1-1)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 05379f44a0a39..971af32efa85c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -124,7 +124,7 @@ AST Dumping Potentially Breaking Changes
     ``__atomic_test_and_set(p, 0)``
 
 - Pretty-printing of templates with inherited (i.e. specified in a previous
-  redeclaration) default arguments has been fixed.
+  redeclaration) and template default arguments has been fixed.
 
 Clang Frontend Potentially Breaking Changes
 -------------------------------------------
diff --git a/clang/lib/AST/DeclPrinter.cpp b/clang/lib/AST/DeclPrinter.cpp
index 7001adeff5397..b2a837b82bb13 100644
--- a/clang/lib/AST/DeclPrinter.cpp
+++ b/clang/lib/AST/DeclPrinter.cpp
@@ -1190,7 +1190,11 @@ void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params,
       VisitNonTypeTemplateParmDecl(NTTP);
     } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
       VisitTemplateDecl(TTPD);
-      // FIXME: print the default argument, if present.
+      if (TTPD->hasDefaultArgument() && !TTPD->defaultArgumentWasInherited()) {
+        Out << " = ";
+        TTPD->getDefaultArgument().getArgument().print(Policy, Out,
+                                                       /*IncludeType=*/false);
+      }
     }
   }
 
diff --git a/clang/test/AST/ast-print-record-decl.c b/clang/test/AST/ast-print-record-decl.c
index fd815881ebeb0..394f837c3645d 100644
--- a/clang/test/AST/ast-print-record-decl.c
+++ b/clang/test/AST/ast-print-record-decl.c
@@ -315,4 +315,11 @@ template <int, int = 0> KW SmearedNTTPDefArgs;
 // PRINT-CXX-NEXT: template <int = 0, int> [[KW]] SmearedNTTPDefArgs;
 template <int = 0, int> KW SmearedNTTPDefArgs;
 
+// PRINT-CXX-LABEL: Tpl
+template <int> KW Tpl;
+// PRINT-CXX-NEXT: template <template <int> class, template <int> class = Tpl> [[KW]] SmearedTplDefArgs;
+template <template <int> class, template <int> class = Tpl> KW SmearedTplDefArgs;
+// PRINT-CXX-NEXT: template <template <int> class = Tpl, template <int> class> [[KW]] SmearedTplDefArgs;
+template <template <int> class = Tpl, template <int> class> KW SmearedTplDefArgs;
+
 #endif
diff --git a/clang/unittests/AST/DeclPrinterTest.cpp b/clang/unittests/AST/DeclPrinterTest.cpp
index 28750c41796d4..2d415c46f15ac 100644
--- a/clang/unittests/AST/DeclPrinterTest.cpp
+++ b/clang/unittests/AST/DeclPrinterTest.cpp
@@ -1090,7 +1090,7 @@ TEST(DeclPrinter, TestClassTemplateDecl9) {
     "template<typename T> struct Z { };"
     "template<template<typename U> class T = Z> struct A { };",
     classTemplateDecl(hasName("A")).bind("id"),
-    "template <template <typename U> class T> struct A {}"));
+    "template <template <typename U> class T = Z> struct A {}"));
 }
 
 TEST(DeclPrinter, TestClassTemplateDecl10) {

Copy link

github-actions bot commented Oct 6, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor Author

@bolshakov-a bolshakov-a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erichkeane, @cor3ntin, @mizvekov

The changes suggested by formatter would cause the TestClassTemplateDecl9 test case to stand out from the neighboring test cases, which use 4-symbol indentation. Should I apply them or put // clang-format off?


- Pretty-printing of templates with inherited (i.e. specified in a previous
redeclaration) default arguments has been fixed.
redeclaration) and template default arguments has been fixed.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is clear and correct.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd advise to add a separate entry to the change log, and say something to the effect you have fixed pretty printing of default arguments to template template parameters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Out << " = ";
TTPD->getDefaultArgument().getArgument().print(Policy, Out,
/*IncludeType=*/false);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default argument handling is similar for all the three kinds. Maybe, it should be a template method VisitTemplateDefArg? Or it is not justified here?

Moreover, maybe, VisitTemplateDecl call and the default argument handling should be united into a VisitTemplateTemplateParmDecl to be similar with the previous two branches?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the other cases are in pretty much the same situation, and they are also only used here.

Either moving all of them inline, or moving this one to a likewise separate function seems fine to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what about

--- a/clang/lib/AST/DeclPrinter.cpp
+++ b/clang/lib/AST/DeclPrinter.cpp
@@ -51,6 +51,15 @@ namespace {
     void PrintObjCTypeParams(ObjCTypeParamList *Params);
     void PrintOpenACCRoutineOnLambda(Decl *D);
 
+    template <typename T>
+    void VisitTemplateDefArg(const T* Arg) {
+      if (Arg->hasDefaultArgument() && !Arg->defaultArgumentWasInherited()) {
+        Out << " = ";
+        Arg->getDefaultArgument().getArgument().print(Policy, Out,
+                                                       /*IncludeType=*/false);
+      }
+    }
+
   public:

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added VisitTemplateTemplateParmDecl method.

@mizvekov
Copy link
Contributor

mizvekov commented Oct 6, 2025

The changes suggested by formatter would cause the TestClassTemplateDecl9 test case to stand out from the neighboring test cases, which use 4-symbol indentation. Should I apply them or put // clang-format off?

You can clang-format the affected region in a separate NFC patch and commit that directly.

bolshakov-a added a commit to bolshakov-a/llvm-project that referenced this pull request Oct 6, 2025
Only TestClassTemplateDecl* tests have been formatted because
TestClassTemplateDecl9 is affected in llvm#162134, and because clang-format
makes the code worse for some other test cases.
mizvekov pushed a commit that referenced this pull request Oct 7, 2025
Only `TestClassTemplateDecl*` tests have been formatted because
`TestClassTemplateDecl9` is affected in #162134, and because
clang-format makes the code worse for some other test cases.
@bolshakov-a bolshakov-a force-pushed the def_tpl_tpl_arg_print branch from 550e585 to 6afe3fe Compare October 7, 2025 06:22
@bolshakov-a
Copy link
Contributor Author

Rebased.

@bolshakov-a
Copy link
Contributor Author

Could someone merge it?

@mizvekov mizvekov merged commit f43721a into llvm:main Oct 7, 2025
10 checks passed
@bolshakov-a bolshakov-a deleted the def_tpl_tpl_arg_print branch October 7, 2025 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants